@echo off
setlocal
set TF="%VS90COMNTOOLS%\..\ide\tf.exe"
set MSBUILD="%windir%\Microsoft.NET\Framework\v3.5\msbuild.exe"
set SSM=%CD%

title Building Scrum Sprint Monitor (started at %DATE% %TIME%)

echo Starting build at %DATE% %TIME%...

@rem Create temp folder
pushd %TEMP%
rd SSMBuild /S /Q
md SSMBuild
cd SSMBuild

@rem Create temp workspace
echo Creating temporary workspace...
%TF% workspace /delete TempBuildScrumSprintMonitor /noprompt
%TF% workspace /new TempBuildScrumSprintMonitor /server:https://tfs08.codeplex.com:443 /comment:"Temporary workspace to get fresh sources to compile Scrum Sprint Monitor" /noprompt

echo Creating temporary workspace mapping...
%TF% workfold /map /workspace:TempBuildScrumSprintMonitor $/scrumsprintmonitor/main .

rem Get the latest
echo Getting lastest changeset into temporary workspace...
%TF% get *.* /recursive

rem Build the latest
echo Building...
%MSBUILD% ScrumSprintMonitor.sln /t:Build /p:Configuration=Release;Platform=x86;TargetFrameworkVersion=v3.5

if errorlevel 1 goto BuildErrorFound

@rem Copy the Release folder to the source folder, cleaning it first
echo Integrating project output...
del "%SSM%\build\x86\Release" /S /Q /F
rd "%SSM%\build\x86\Release" /S /Q
echo Primed target folder.
echo Copying build result to target folder...
xcopy build\x86\Release "%SSM%\build\x86\Release" /I /S
echo Build result is in place on target folder.

@rem Delete temp workspace
echo Deleting temporary workspace...
%TF% workspace /delete TempBuildScrumSprintMonitor /noprompt

@rem Remove temp folder
echo Removing temporary folder...
cd ..
rd SSMBuild /S /Q
popd

echo Finished building project at %DATE% %TIME%.
goto Exit

:Exit
echo on
@exit

:BuildErrorFound

@rem Remove temp folder
echo Removing temporary folder...
cd ..
rd SSMBuild /S /Q
popd

echo Build failed!
echo on